﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using codefile;

namespace ArleneBarandGrill
{
    public partial class Form3 : Form
    {
        DataTable DTbl = new DataTable();
        Data ms = new Data();
        UserList users = new UserList();

        int curIndex;

        public Form3()
        {
            InitializeComponent();
            btnAdd.Click += new EventHandler(btnAdd_Click);
            this.Load += new EventHandler(Form1_Load);
            txtSearc.TextChanged += new EventHandler(txtSearc_TextChanged);
            btnUpdate.Click += new EventHandler(btnUpdate_Click);
            DGV1.DoubleClick += new EventHandler(DGV1_DoubleClick);
        }

        void DGV1_DoubleClick(object sender, EventArgs e)
        {
            curIndex = DGV1.SelectedRows[0].Index;
            txtUserID.Text = ms.DTbl.Rows[curIndex][0].ToString();
            txtUS.Text = ms.DTbl.Rows[curIndex][1].ToString();
            txtPS.Text = ms.DTbl.Rows[curIndex][2].ToString();
            txtName.Text = ms.DTbl.Rows[curIndex][3].ToString();
            txtLVL.Text = ms.DTbl.Rows[curIndex][4].ToString();
        }

        void btnUpdate_Click(object sender, EventArgs e)
        {
            UserList uslist = new UserList();
            uslist.fID = txtUserID.Text;
            uslist.fUsername = txtUS.Text;
            uslist.fPassword = txtPS.Text;
            uslist.fNameofEmployees = txtName.Text;
            uslist.fLevelsOfAccess = txtLVL.Text;

            ms.edituser(uslist, curIndex);
            Form1_Load(null, null);
        }

        void txtSearc_TextChanged(object sender, EventArgs e)
        {
            if (txtSearc.Text != "")
            {
                ms.connect();
                string sql = "SELECT * FROM tblusers WHERE fID LIKE '%" + txtSearc.Text + "%' OR fUsername LIKE '%" + txtSearc.Text + "%' OR fPassword LIKE '%" + txtSearc.Text + "%' OR fNameofEmployees LIKE '%" + txtSearc.Text + "%' OR fLevelsOfAccess LIKE '%" + txtSearc.Text + "%'";
                ms.querry(sql);
                DTbl = ms.DTbl;
                DGV1.DataSource = DTbl;
                ms.close();
            }
        }

        void Form1_Load(object sender, EventArgs e)
        {
            ms.connect();
            ms.querry("SELECT * FROM tblusers");
            DTbl = ms.DTbl;
            DGV1.DataSource = DTbl;
            ms.close();
        }

        void btnAdd_Click(object sender, EventArgs e)
        {
            users.fID = txtUserID.Text;
            users.fUsername = txtUS.Text;
            users.fPassword = txtPS.Text;
            users.fNameofEmployees = txtName.Text;
            users.fLevelsOfAccess = txtLVL.Text;

            ms.adduser(users);

            MessageBox.Show("Successful");

            txtUserID.Text = "";
            txtUS.Text = "";
            txtPS.Text = "";
            txtName.Text = "";
            txtLVL.Text = "";
        }
    }
}
